package com.qianli.cashmama.trade.repo.impl;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.api.Page;
import com.fqgj.common.utils.CollectionUtils;
import com.qianli.cashmama.trade.common.enums.TimeoutTypeEnum;
import com.qianli.cashmama.trade.dao.TTimeoutCenterDao;
import com.qianli.cashmama.trade.domain.model.TimeoutEvent;
import com.qianli.cashmama.trade.entity.TTimeoutCenterEntity;
import com.qianli.cashmama.trade.repo.TimeoutRepository;
import com.robert.vesta.util.IdUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/7/24
 * Time: 下午2:05
 */
@Repository
public class TimeoutRepositoryImpl implements TimeoutRepository {
    private static final Logger logger = LoggerFactory.getLogger(TimeoutRepositoryImpl.class);
    @Autowired
    private TTimeoutCenterDao timeoutCenterDao;

    @Override
    public void store(TimeoutEvent timeoutEvent) {
        TTimeoutCenterEntity tTimeoutCenterEntity = new TTimeoutCenterEntity()
                .setType(timeoutEvent.getType().getType())
                .setTriggerTime(timeoutEvent.getTriggerTime())
                .setValue(timeoutEvent.getValue());

        IdUtils.genId();
//        tTimeoutCenterEntity.setId(IdCenterUtil.getSequenceNo(TradeBizCodeEnum.TIMEOUT_CENTER));
        timeoutCenterDao.insert(tTimeoutCenterEntity);
    }

    @Override
    public List<TimeoutEvent> selectLatestListByPage(Page page, Integer type, Integer shardingItem) {
        List<TTimeoutCenterEntity> timeoutCenterEntityList = timeoutCenterDao.selectListByPageAndDate(page, new Date(), type, shardingItem);
        logger.info("timeoutCenterEntityList size:{},page{},date{}", timeoutCenterEntityList == null ? null : timeoutCenterEntityList.size(), JSON.toJSON(page), new Date());
        if (CollectionUtils.isEmpty(timeoutCenterEntityList)) {
            return null;
        }
        List<TimeoutEvent> timeoutEventList = new ArrayList<>(timeoutCenterEntityList.size());
        for (TTimeoutCenterEntity entity : timeoutCenterEntityList) {
            TimeoutTypeEnum typeEnum = TimeoutTypeEnum.conventByType(entity.getType());
            if (typeEnum == null) {
                continue;
            }
            TimeoutEvent timeoutEvent = new TimeoutEvent()
                    .setId(entity.getId())
                    .setTriggerTime(entity.getTriggerTime())
                    .setType(typeEnum)
                    .setValue(entity.getValue());

            timeoutEventList.add(timeoutEvent);
        }

        return timeoutEventList;
    }

    @Override
    public void delete(TimeoutEvent timeoutEvent) {
        TTimeoutCenterEntity tTimeoutCenterEntity = new TTimeoutCenterEntity()
                .setType(timeoutEvent.getType().getType())
                .setTriggerTime(timeoutEvent.getTriggerTime())
                .setValue(timeoutEvent.getValue());
        tTimeoutCenterEntity.setId(timeoutEvent.getId());
        tTimeoutCenterEntity.setDeleted(true);

        timeoutCenterDao.updateByPrimaryKey(tTimeoutCenterEntity);
    }
}
